<?php
class TagController extends BaseController {
	
	protected function _init_() {
		$this->addTab(__('标签列表'), 'tag.index');
		$this->addTab(__('编辑标签'), 'tag.edit');
	}
	
	//标签列表
	public function indexAction($params = array()) {
		$min = intval($this->input->min);
		$max = intval($this->input->max);
		$keyword = String::safeStr($this->input->keyword);
		$page = max(1, intval($params['page']));
		$pagesize = 20;
		$wheresql = '1';
		if ($keyword) $wheresql .= " AND tagname LIKE '%$keyword%'";
		if ($min > 0) $wheresql .= " AND `count` >= '$min'";
		if ($max > 0) $wheresql .= " AND `count` <= '$max'";
		$count = $this->db->count($this->db->getTable('tag'), $wheresql);
		$list = array();
		if ($count > 0) {
			$sql = "SELECT * FROM ".$this->db->getTable('tag')." WHERE $wheresql ORDER BY id DESC";
			$list = $this->db->getList($sql, $page, $pagesize);
		}
		$pager = new Pager($count, $page, compact('min','max','keyword'));
		$pagebar = $pager->makeHtml();
		$this->assign(compact('min', 'max', 'keyword', 'list', 'pagebar'));
		$this->display();
	}
	
	//编辑标签
	public function editAction() {
		if ($this->input->isPost()) {
			$id = intval($this->input->id);
			$tagname = $this->input->tagname;
			if (empty($tagname)) $this->prompt(__('标签名称不能为空'));
			$tag = M('Tag', $id);
			if ($tag->exists) {
				$tag->tagname = $tagname;
				if (!$tag->save()) {
					$this->prompt($tag->error);
				}
			}
			$this->redirect(URL('tag.index'));
		} else {
			$tag = array();
			if (is_numeric($this->input->id)) {
				$tag = M('Tag', $this->input->id);
				$tag = $tag->toArray();
			}
			$this->assign('tag', $tag);
		}
		$this->display();
	}
	
	//重新统计标签文章数
	public function recountAction() {
		$ids = $this->input->id;
		if (!is_array($ids)) $ids = array($ids);
		foreach ($ids as $id) {
			if (is_numeric($id) && $id > 0) {
				$tag = M('Tag', $id);
				if ($tag->exists) {
					$count = $this->db->count($this->db->getTable('tagpost'), "tagid = {$id}");
					$tag->count = intval($count);
					$tag->save();
				}
			}
		}
		$this->redirect($this->refer);
	}
	
	//删除标签
	public function deleteAction() {
		$ids = $this->input->id;
		if (!is_array($ids)) $ids = array($ids);
		$ids = array_map('intval', $ids);
		$this->db->execute("DELETE FROM ".$this->db->getTable('tag')." WHERE id IN (".String::simplode($ids).")");
		$this->db->execute("DELETE FROM ".$this->db->getTable('tagpost')." WHERE tagid IN (".String::simplode($ids).")");
		$this->redirect($this->refer);
	}
	
	//合并标签
	public function mergeAction() {
		$newtag = String::safeStr($this->input->newtag);
		$ids = $this->input->id;
		if (!is_array($ids)) $ids = array($ids);
		if ( strlen($newtag) < 1 || strlen($newtag) > 20 ) {
			$this->prompt(__('标签长度应在1-20字节之间'));
		}
		$tagid = $this->db->getOne("SELECT id FROM ".$this->db->getTable('tag')." WHERE tagname = '$newtag'");
		if ( !$tagid ) {
			$tagid = $this->db->add(array('tagname'=>$newtag), $this->getTable('tag'));
		}
		$upids = array();
		foreach ($ids as $id) {
			if (is_numeric($id) && $id != $tagid && !in_array($id, $upids)) {
				$upids[] = $id;
			}
		}
		if ($upids) {
			$this->db->update(array('tagid'=>$tagid), array('tagid'=>$upids), $this->db->getTable('tagpost'));
			$this->db->delete(array('id'=>$upids), $this->db->getTable('tag'));
		}
		$this->redirect($this->refer);
	}
}
